<?php
/**
 * Sabai_Application
 */
require 'Sabai/Application.php';

class Sabai_Application_Model extends Sabai_Application
{
    /**
     * @access private
     * @var string
     */
    var $_dbScheme;
    /**
     * @access private
     * @var string
     */
    var $_dbClientEncoding;
    /**
     * @access private
     * @var string
     */
    var $_dbPrefix = '';
    /**
     * @access private
     * @var array
     */
    var $_dbOptions;
    /**
     * @access private
     * @var string
     */
    var $_modelPrefix = '';
    /**
     * @access private
     * @var string
     */
    var $_userScheme;
    /**
     * @access private
     * @var array
     */
    var $_authOptions = array();
    /**
     * @access private
     * @var array
     */
    var $_identityOptions = array();

    /**
     * Initializes the application
     *
     */
    function Sabai_Application_Model($name, $path)
    {
        parent::Sabai_Application($name, $path);

        $this->_dbClientEncoding = SABAI_CHARSET;

        // for mysql
        $this->_dbScheme = 'mysql';
        $this->_dbOptions = array('host'      => 'localhost:3307',
                                  'dbname'    => '',
                                  'user'      => 'root',
                                  'pass'      => '');

        // for postgresql
        //$this->_dbScheme = 'PostgreSQL';
        //$this->_dbOptions = array('host'   => 'localhost',
        //                          'dbname' => '',
        //                          'user'   => 'root',
        //                          'pass'   => '',
        //                          'port'   => '5432');

        // for sqlite
        //$this->_dbScheme = 'SQLite';
        //$this->_dbOptions = array('dbname' => '');

        $this->_modelPrefix = $this->_name . '_Model_';
    }

    function setDBScheme($dbScheme)
    {
        $this->_dbScheme = $dbScheme;
    }

    function setDBClientEncoding($dbClientEncodiing)
    {
        $this->_dbClientEncoding = $dbClientEncodiing;
    }

    function setDBPrefix($dbPrefix)
    {
        $this->_dbPrefix = $dbPrefix;
    }

    function setDBOptions($dbOptions)
    {
        $this->_dbOptions = $dbOptions;
    }

    function setModelPrefix($modelPrefix)
    {
        $this->_modelPrefix = $modelPrefix;
    }

    function setUserScheme($userScheme)
    {
        $this->_userScheme = $userScheme;
    }

    function setUserOptions($authOptions, $identityOptions)
    {
        $this->_authOptions = $authOptions;
        $this->_identityOptions = $identityOptions;
    }

    function &_getAuthenticator()
    {
        require_once 'Sabai/User/AuthenticatorFactory.php';
        $authenticator =& Sabai_User_AuthenticatorFactory::create($this->_userScheme, $this->_authOptions);
        return $authenticator;
    }

    function &_getIdentityFetcher()
    {
        require_once 'Sabai/User/IdentityFetcherFactory.php';
        $identity_fetcher =& Sabai_User_IdentityFetcherFactory::create($this->_userScheme, $this->_identityOptions);
        return $identity_fetcher;
    }

    function &_getDB()
    {
        require_once 'Sabai/DB.php';
        $db =& Sabai_DB::factory($this->_dbScheme, $this->_dbPrefix, $this->_dbClientEncoding, $this->_dbOptions);
        return $db;
    }

    function &_getModel()
    {
        require_once 'Sabai/Model.php';
        $model_dir = $this->_path . '/Model';
        $model =& new Sabai_Model($model_dir, $this->_modelPrefix);
        $model->setDB($this->getService('DB'));
        $model->setUserIdentityFetcher($this->getService('IdentityFetcher'));
        return $model;
    }
}